package com.sc.byahi.user.client;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.sc.byahi.user.shared.ProfileMetadata;

public class Summary extends Composite {

	private static SummaryUiBinder uiBinder = GWT.create(SummaryUiBinder.class);

	interface SummaryUiBinder extends UiBinder<Widget, Summary> {
	}

	@UiField
	Hyperlink profileId;
	@UiField
	Hyperlink interestSent;
	@UiField
	Hyperlink interestAccepted;
	@UiField
	Hyperlink interestOnHold;
	@UiField
	Hyperlink reqReceived;
	@UiField
	Hyperlink reqAccepted;
	@UiField
	Hyperlink reqOnHold;

	private LinkedHashMap<String, Hyperlink> map = new LinkedHashMap<String, Hyperlink>();

	public void cleanForReuse() {
		profileId.setText("-");
		for (Hyperlink link : map.values()) {
			link.setText("");
		}
	}

	public Summary() {
		initWidget(uiBinder.createAndBindUi(this));
		map.put(ProfileMetadata.IS, interestSent);
		map.put(ProfileMetadata.IA, interestAccepted);
		map.put(ProfileMetadata.IH, interestOnHold);
		map.put(ProfileMetadata.RR, reqReceived);
		map.put(ProfileMetadata.RA, reqAccepted);
		map.put(ProfileMetadata.RH, reqOnHold);
	}

	public void setData(Entry<Long, Map<String, Set<Long>>> entry) {
		profileId.setText("Profile ID: " + entry.getKey().toString());
		profileId.setTargetHistoryToken("my_profile_id" + ":" + entry.getKey());
		setLinkText(entry);
	}

	private void setLinkText(Entry<Long, Map<String, Set<Long>>> entry) {
		for (String linkType : map.keySet()) {
			Hyperlink link = map.get(linkType);
			int size = entry.getValue().get(linkType).size();
			link.setTargetHistoryToken(linkType + ":" + entry.getKey());
			if (size > 0) {
				link.setText(size + "");
				link.setStyleName("red-link");
			}else{
				link.setText("");
			}
		}
	}
}
